﻿using System.Text.Json;
using System.Text.Json.Serialization;

namespace Helper
{
    public class ToolHelper
    {
        public static string PrintObjectRecursive(object obj)
        {
            if (obj == null) return "null";

            try
            {
                var options = new JsonSerializerOptions
                {
                    WriteIndented = true,      // 格式化输出
                    ReferenceHandler = ReferenceHandler.IgnoreCycles,  // 处理循环引用
                    MaxDepth = 10              // 最大递归深度
                };

                return JsonSerializer.Serialize(obj, options);
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }


        public static string GetValueOrDefault(Dictionary<string, Object> dictionary, string key, string defaultValue = "-")
        {
            if (dictionary == null || !dictionary.TryGetValue(key, out var value) || value == null)
            {
                return defaultValue;
            }

            // 特殊处理字符串类型：空字符串视为默认值
            if (value is string str && string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }

            return value.ToString();
        }
    }
}
